package com.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class LoadIcon extends Sprite
	{
		private static var _parentContain:DisplayObjectContainer;
		private var _stageWidth:int;
		private var _stageHeight:int;
		private var  _thisSize:uint ;//父容器最大正方形区域
		private var _circleList:Vector.<Circle>;//circle数组
		private var _circleNum:int = 5;//circle数量
		private var _moveRadius:uint;//圆周半径
		private var _circleAngel:int; //circle位置
		private var _addAngle:int = 0;//递加角度
		private var _circleRadius:int = 0;
		private var _circleCenter:Point = new Point();
		private var _circleContain:Sprite;
		private var _loadProgress:Number;
		private var _txtProgress:TextField;
		private var _txtTip:TextField;
		
		public function LoadIcon(parentContain:DisplayObjectContainer,contentWidth:int = 0,contentHeight:int = 0)
		{
			_parentContain  = parentContain;
			_stageWidth = contentWidth;
			_stageHeight = contentHeight;
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
		}
		
		private function onAddToStageHandler(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStageHandler);
			initContain();
			initCircle();
		}
		
		private function initContain():void
		{
			this.mouseChildren = false;
			this.mouseEnabled = false;
			_circleContain = new Sprite();
			this.addChild(_circleContain);
		}
		
		private function initCircle():void
		{
			_thisSize = Math.min(_stageWidth,_stageHeight);
			_thisSize > 0 ? _thisSize = _thisSize : _thisSize = 30;
			_moveRadius = _thisSize / 4 > 30 ? 30 : _thisSize / 4;
			_circleAngel = int(180 / _circleNum) ;
			_circleRadius = int(_moveRadius / _circleNum);
			_circleCenter.x = _stageWidth == 0 ? _thisSize/2 : _stageWidth/2;
			_circleCenter.y =_stageHeight == 0 ? _thisSize/2 : _stageHeight/2;
			setCircle();
		}
		
		private function setCircle():void
		{
			_circleList = new Vector.<Circle>();
			for (var i:int = 1; i <= _circleNum; i++) 
			{
				var circle:Circle = new Circle(i*(1.5/_circleNum),_circleRadius);
				_circleContain.addChild(circle);
				circle.x = _moveRadius*Math.cos(((_circleNum-i)*_circleAngel + _addAngle)*Math.PI/180) + _circleCenter.x;
				circle.y = -_moveRadius*Math.sin(((_circleNum-i)*_circleAngel  + _addAngle)*Math.PI/180) + _circleCenter.y;
				_circleList.push(circle);
			}
			this.addEventListener(Event.ENTER_FRAME,onLoopHandler);
		}
		
		private function onLoopHandler(e:Event):void
		{
			circleMove();
		}		
		
		/**圆周运动*/		
		private function circleMove():void
		{
			_addAngle -= 5;
			for (var i:int = 0; i < _circleList.length; i++) 
			{
				_circleList[i].x = _moveRadius*Math.cos(((_circleNum-i)*_circleAngel + _addAngle)*Math.PI/180) + _circleCenter.x;
				_circleList[i].y = -_moveRadius*Math.sin(((_circleNum-i)*_circleAngel  + _addAngle)*Math.PI/180) + _circleCenter.y;
			}
		}
		
		/**显示加载进度*/		
		public function set loadProgress(value:Number):void
		{
			_loadProgress = value;
			if(_loadProgress >= 100) _loadProgress = 100;
			if(!_txtProgress) addProgressShow();
			setTxtContain();
		}
		
		private function setTxtContain():void
		{
			_txtProgress.htmlText = htmlTxt(_loadProgress) + htmlTxt('%');
			_txtProgress.x = _circleCenter.x - _txtProgress.textWidth/2;
			_txtProgress.y = _circleCenter.y - _txtProgress.textHeight/2;
		}
		
		private function htmlTxt(source:*,color:String = '#FFFFFF',size:uint = 14,font:String = 'Verdana'):String
		{
			return '<font color="' + color +'" size="' + size + '" font="' + font +'"><b>' + source + '</b></font>'
		}
		
		private function addProgressShow():void
		{
			if(!this.parent) return;
			_txtProgress = new TextField();
			this.addChild(_txtProgress);
			_txtTip = new TextField();
			this.addChild(_txtTip);
			_txtTip.htmlText = htmlTxt('正在努力加载资源,请稍候...');
			_txtTip.width = _txtTip.textWidth + 20;
			_txtTip.x = _circleCenter.x - _txtTip.textWidth / 2;
			_txtTip.y = _circleCenter.y + _circleContain.height / 2 +_txtTip.textHeight + 20;
		}
		
		
		
		
		private function onRemoveFromStageHandler(e:Event = null):void
		{
			this.removeEventListener(Event.ENTER_FRAME,onLoopHandler);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStageHandler);
			for (var i:int = 0; i < _circleList.length; i++) 
			{
				clearTarget(_circleList[i]);
			}
			_circleList = new Vector.<Circle>();
			while(_circleContain.numChildren) _circleContain.removeChildAt(0);
			clearTarget(_circleContain);
			if(_txtProgress) clearTarget(_txtProgress);
			if(_txtTip) clearTarget(_txtTip);
		}
		
		private function clearTarget(target:DisplayObject):void{
			target.parent.removeChild(target);
			target = null;
		}
		
	}
}
import flash.display.Sprite;

class Circle extends Sprite{
	public function Circle(alp:Number=1,radius:int = 10,color:uint = 0xFFFFFF){
		this.graphics.clear();
		this.graphics.beginFill(color,alp);
		this.graphics.drawCircle(0,0,radius);
		this.graphics.endFill();
	}
}